package com.elvin.zoe.parser.call;

import java.util.ArrayList;
import java.util.List;

import com.elvin.zoe.pojo.Message;

public class CallLogGmailParser extends CallLogParser {

	@Override
    protected String getMessagingWith(String originalHtml) {
        String token = "Call with ";
        int tokenStart = originalHtml.indexOf(token) + token.length();
        int tokenEnd = originalHtml.indexOf('<', tokenStart);
        return originalHtml.substring(tokenStart, tokenEnd);
    }

	@Override
    protected List<String> getMessageTokens(String originalHtml) {
        String messageStartToken = "<hr><table width=100% cellpadding=0 cellspacing=0 border=0>";
        String messageEndToken = "</table></table>";
        List<String> messageTokens = new ArrayList<String>();
        int tokenStart = originalHtml.indexOf(messageStartToken);
        while (tokenStart > 0) {
            int tokenEnd = originalHtml.indexOf(messageEndToken, tokenStart) + messageEndToken.length();
            messageTokens.add(originalHtml.substring(tokenStart, tokenEnd));
            tokenStart = originalHtml.indexOf(messageStartToken, tokenEnd);
        }
        return messageTokens;
    }

	@Override
    protected int getMessageType(String messageToken) {
        String typeToken = "telenths@gmail.com&lt;telenths@gmail.com&gt;";
        if (messageToken.indexOf(typeToken) >= 0) {
            return Message.OUTGOING;
        } else {
            return Message.INCOMMING;
        }
    }

	@Override
    protected String getMessageDatetime(String messageToken) {
        String datetimeStartToken = "<td align=right><font size=-1><b>";
        int tokenStart = messageToken.indexOf(datetimeStartToken) + datetimeStartToken.length();
        int tokenEnd = messageToken.indexOf("</b>", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }

	@Override
	protected String getMessageDatetimeFormat() {
		return "EEE, MMM d, yyyy 'at' hh:mm aaa";
	}
	
	@Override
    protected int getMessageCallDuration(String messageToken) {
        String textStartToken = "<font size=-1>";
        int tokenStart = messageToken.lastIndexOf(textStartToken) + textStartToken.length();
        int tokenEnd = messageToken.indexOf("</font>", tokenStart);
        
        String callTimeMessage = messageToken.substring(tokenStart, tokenEnd).replace("<font color=#550055>", "");
        int callTimeTokenStart = callTimeMessage.indexOf('(');
        String callTimeString = callTimeMessage.substring(0, callTimeTokenStart - 2);
        
        return Integer.parseInt(callTimeString);
    }

}
